/*
 * Copyright (c) 2011 Skullab software @ Ivan Maruca
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.skullab.util;

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public abstract class EulaActivity extends Activity implements EulaInterface{

	/**
	 * Used for localization (see {@link #localeEulaContentFromAssets(String, String, int)})
	 * Specific the country code to use for search directory
	 */
	protected static final int LOCALE_COUNTRY = 0 ;
	/**
	 * Used for localization (see {@link #localeEulaContentFromAssets(String, String, int)})
	 * Specific the language code to use for search directory
	 */
	protected static final int LOCALE_LANGUAGE = 1 ;
	/**
	 * Used for localization (see {@link #localeEulaContentFromAssets(String, String, int)})
	 * Specific the language code and the country code to use for search directory
	 */
	protected static final int LOCALE_LANGUAGE_COUNTRY = 2 ;
	
	private static final int NO_LOCALE = 100 ;
	
	private EulaActivity mReference ;
	private TextView eulaTitle,eulaSubTitle,eulaContent ;
	private Button bAccept,bRefuse ;
	private ImageView eulaIcon ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setTheme(android.R.style.Theme_Dialog);
		
		super.onCreate(savedInstanceState);
		mReference = this ;
		this.onSetContentView();
		this.onItializing();
		this.onButtonsSet();
		this.onLoad();
	}
	
	@Override
	protected void onStart(){
		super.onStart();
	}
	
	@Override
	protected void onResume(){
		super.onResume();
	}
	
	@Override
	protected void onPause(){
		super.onPause();
	}
	
	@Override
	protected void onStop(){
		super.onStop();
	}
	
	@Override
	protected void onRestart(){
		super.onRestart();
	}
	
	@Override
	protected void onDestroy(){
		super.onDestroy();
	}
	
	@Override
	public void onBackPressed(){
		// NO ESCAPE !
	}
	
	//*************************************************************************************************
	protected void onSetContentView(){
		setContentView(R.layout.ui_layout);
	}
	
	protected void onItializing(){
		eulaTitle = (TextView)findViewById(R.id.title_eula);
		eulaSubTitle = (TextView)findViewById(R.id.title_eula_spec);
		eulaContent = (TextView)findViewById(R.id.body_eula_content);
		
		bAccept = (Button)findViewById(R.id.button_accept);
		bRefuse = (Button)findViewById(R.id.button_refuse);
		
		eulaIcon = (ImageView)findViewById(R.id.ic_eula);
	}
	
	protected void onError(){}
	
	//*************************************************************************************************
	
	//***********************
	// PUBLIC METHODS
	//***********************
	
	/**
	 * Used to set image icon of the Eula
	 * @param resid The resource identifier of the drawable
	 */
	public void setImageIcon(int resid){
		eulaIcon.setImageResource(resid);
	}
	
	/**
	 * Used to set image icon of the Eula
	 * @param image The bitmap to set
	 */
	public void setImageIcon(Bitmap image){
		eulaIcon.setImageBitmap(image);
	}
	
	/**
	 * Used to set image icon of the Eula
	 * @param image The drawable to set
	 */
	public void setImageIcon(Drawable image){
		eulaIcon.setImageDrawable(image);
	}
	
	/**
	 * Used to set image icon of the Eula
	 * @param uriImage The uri of an image
	 */
	public void setImageIcon(Uri uriImage){
		eulaIcon.setImageURI(uriImage);
	}
	
	/**
	 * Used to set the EULA content
	 * @param resid The resource id
	 */
	public void setEulaContent(int resid){
		eulaContent.setText(resid);
	}
	
	/**
	 * Used to set the EULA content
	 * @param content The string content
	 */
	public void setEulaContent(String content){
		eulaContent.setText(content);
	}
	
	/**
	 * Used to set the EULA content from .txt file stored in assets directory
	 * The name of the file MUST contain the extension (es. "myeula.txt") 
	 * @param filename The .txt file name. 
	 */
	public void setEulaContentFromAssets(String filename){
		String content = "";
		try{
			InputStream is = this.getAssets().open(filename);
			int size = is.available();
			byte[] buffer = new byte[size] ;
			is.read(buffer);
			is.close();
			content = new String(buffer);
		}catch(Exception exc){
			onError();
		}finally{
			eulaContent.setText(content);
		}
	}
	
	/**
	 * Used to set the EULA content from .txt file stored in assets directory but using localization.
	 * To localize your EULA create a series of directory in assets using underscore to seperate language code and/or country code
	 * Example:
	 * Create a generic directory "eula" in assets, then create other "eula" directory about other language and/or country
	 * for example to localize english version create "eula_es" or for specific region for example United States, 
	 * create "eula_en_US" direcotry. You can create a language version or country or both !
	 * <br><br>
	 * Tip: If using mode {@link #LOCALE_LANGUAGE_COUNTRY} and the device is no localize for the series of directory that you have created,
	 * The method search automatically only for language, if not, automatically switch to generic directory.
	 * 
	 * @param path The generic path
	 * @param filename The file name
	 * @param mode The mode to localize {@link #LOCALE_COUNTRY} , {@link #LOCALE_LANGUAGE} , {@link #LOCALE_LANGUAGE_COUNTRY}
	 */
	public void localeEulaContentFromAssets(String path,String filename,int mode){
		localeEulaContentFromAssets(path,filename,mode,0);
	}
	/**
	 * Returns the country code for this locale, or "" if this locale doesn't correspond to a specific country.
	 * The country codes are two-letter uppercase ISO country codes (such as "US") as defined by ISO 3166-1.
	 */
	public String getCountryCode(){
		return this.getResources().getConfiguration().locale.getCountry() ;
	}
	/**
	 * Returns the language code for this Locale or the empty string if no language was set.
	 * The language codes are two-letter lowercase ISO language codes (such as "en") as defined by ISO 639-1.
	 */
	public String getLanguageCode(){
		return this.getResources().getConfiguration().locale.getLanguage();
	}
	//***********************
	// PRIVATE DECLARATIONS
	//***********************
	
	private void onButtonsSet(){
		
		bAccept.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				mReference.onAccept();
			}});
		
		bRefuse.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				mReference.onRefuse();
			}});
	}
	
	private boolean localeEulaContentFromAssets(String path,String filename,int mode,int error){
		
		String content = "";
		
		String mPath = path ;
		
		if(error == NO_LOCALE && mode == LOCALE_LANGUAGE_COUNTRY){
			return localeEulaContentFromAssets(mPath, filename,LOCALE_LANGUAGE,0);
		}
		
		if(error != NO_LOCALE){
			switch(mode){
			case LOCALE_COUNTRY :
				path += "_" + getCountryCode() ;
				break;
			case LOCALE_LANGUAGE :
				path += "_" + getLanguageCode() ;
				break;
			case LOCALE_LANGUAGE_COUNTRY:
				path += "_" + getLanguageCode() + "_" + getCountryCode() ;
			}
		}
		
		path += "/" + filename ;
		Log.d("EULA","path and filename : " +path);
		try{
			InputStream is = this.getAssets().open(path);
			int size = is.available();
			byte[] buffer = new byte[size] ;
			is.read(buffer);
			is.close();
			content = new String(buffer);
			eulaContent.setText(content);
			return true ;
		}catch(IOException exc){
			if(error != NO_LOCALE){
				localeEulaContentFromAssets(mPath,filename,mode,NO_LOCALE);
			}else onError();
			
			return false ;
		}
	}
}
